import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import { logger } from './common/middleware/logger.middleware';
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
import { ResponseInterceptor } from './common/interceptor/response.interceptor';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

async function bootstrap() {
  // const app = await NestFactory.create(AppModule);
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 处理跨域
  app.enableCors();

  // 全局中间件
  app.use(logger);

  //开启一个全局验证管道
  app.useGlobalPipes(new ValidationPipe());

  // 全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter());

  // 配置全局拦截器
  app.useGlobalInterceptors(new ResponseInterceptor());

  // 项目开发一般分开发环境、测试环境、生产测试环境和正式环境，正式环境屏蔽。
  const options = new DocumentBuilder()
  .setTitle('OKR-nest接口文档')
  .setDescription('OKR常用接口汇总') // 文档介绍
  .setVersion('1.0.0') // 文档版本
  .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('/swagger', app, document);

  await app.listen(8001);
}
bootstrap();
